package com.education.common.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {


    public static final String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy.MM.dd");
    public static SimpleDateFormat dateFormatDB = new SimpleDateFormat("yyyyMMdd");// 数据库使用的日期格式
    public static SimpleDateFormat dataTimeFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
    public static SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat dataTimeFormat1 = new SimpleDateFormat("yyyy.MM.dd HH:mm");
    public static SimpleDateFormat yyyyMMddHHmmssDateTimeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    public static SimpleDateFormat dateMonthFormatDB = new SimpleDateFormat("yyyyMM");// 数据库使用的日期格式
    public static SimpleDateFormat dateMonthFormatYYYY_MM = new SimpleDateFormat("yyyy-MM");// 数据库使用的日期格式
    public static SimpleDateFormat formatWW = new SimpleDateFormat("yyyy-ww"); //周日期
    public static SimpleDateFormat PARSE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //周日期

    public static Date getInialDateTime() {
        Date parse = null;
        String s = DateUtil.dateFormat2(new Date());
        String s2 = new StringBuffer().append(s).append(" 00:00:00").toString();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            parse = simpleDateFormat.parse(s2);
            //System.out.println(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 比较时间大小
     *
     * @param date   要比较时间
     * @param isDate 被比较时间
     * @return
     */
    public static boolean isFlag(Date date, Date isDate) {
        return date.getTime() > isDate.getTime();
    }

    public static String dateFormat(Date date) {
        return dateFormat1.format(date);
    }


    public static String dateFormat2(Date date) {
        return dateFormat.format(date);
    }

    public static String format(Date date) {
        return FORMAT.format(date);
    }

    public static String getCurrentTime() {
        Date date = new Date();
        return dataTimeFormat.format(date);
    }

    public static Date parse(String date) {
        try {
            return PARSE.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String dataTimeFormat(Date date) {
        return dataTimeFormat.format(date);
    }

    public static String parseTime(Date date) {
        return dataTimeFormat.format(date);
    }

    /**
     * 功能：指定日期减去指定天数
     *
     * @param date
     * @param day
     * @return
     */
    public static Date diffDate(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
        return c.getTime();
    }


    /**
     * 功能：指定日期之前指定天数的所有日期的集合
     *
     * @param date
     * @param day
     * @return
     */
    public static List<Date> diffDateList(Date date, int day) {
        List<Date> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        for (int i = 0; i < day; i++) {
            c.setTimeInMillis(getMillis(date) - ((long) i) * 24 * 3600 * 1000);
            list.add(c.getTime());
        }
        return list;
    }

    /**
     * 转换日期格式  yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String getDate(Date date) {
        return dateFormat.format(date);
    }

    /**
     * 功能：获取当前日期 格式:2008-02-02
     *
     * @return
     */
    public static String getCurrentDateTime() {
        Date date = new Date();
        return dateFormat.format(date);
    }

    /**
     * @param dateStr yyyyMMdd 格式的日期
     * @return
     */
    public static Date parseDateFormatDB(String dateStr) {
        Date date = null;
        try {
            date = dateFormatDB.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * 生日计算年龄
     *
     * @param date
     * @return
     */
    public static int birthdateToAge(Date date) {
        int reVal = 0;
        int numOne = 0;
        int numTwo = 0;
        Calendar calOne = Calendar.getInstance();
        Calendar calTwo = Calendar.getInstance();
        calOne.setTime(date);
        calTwo.setTime(new Date());
        numTwo = calTwo.get(1);
        numOne = calOne.get(1);
        reVal = numTwo - numOne;
        return reVal;
    }

    /**
     * 功能：生成日期yyyyMMdd
     *
     * @return
     */
    public static String getDbDate() {
        return dateFormatDB.format(new Date());
    }

    /**
     * 功能：生成日期yyyy-MM-dd
     *
     * @return
     */
    public static String getdateFormatDbDate() {
        return dateFormat.format(new Date());
    }

    /**
     * 功能：把日期yyyy-MM-dd格式转换成yyyyMMDD日期格式
     *
     * @param dateStr
     * @return
     */
    public static String convertClientDateToDbDate(String dateStr) {
        String dbDateStr = null;
        try {
            dbDateStr = dateFormatDB.format(dateFormat.parse(dateStr));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dbDateStr;
    }

    /**
     * 功能：解析数据库中的日期字符串 格式:yyyy-MM-dd
     *
     * @param dateStr
     * @return
     */
    public static Date parseDate(String dateStr) {
        Date date = null;
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 功能：格式化日期字符串 例如：2008-8-8 转换为2008-08-08
     *
     * @param dateStr
     * @return
     */
    public static String getDateStrFormat(String dateStr) {
        try {
            Date date = dateFormat.parse(dateStr);
            return dateFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 功能：解析数据库中的时间字符串 格式:yyyy-MM-dd HH:mm:ss
     *
     * @param
     * @return
     */
    public static Date parseDateTime(String dateTimeStr) {
        Date date = null;
        try {
            date = dataTimeFormat.parse(dateTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 计算两个日期之间的天数
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    public static int calcDays(String startDate, String endDate) {
        int days = 1;
        try {
            long start = dateFormat.parse(startDate).getTime();
            long end = dateFormat.parse(endDate).getTime();
            days = (int) ((end - start) / (24 * 60 * 60 * 1000));
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
        return days;
    }

    /**
     * 功能：指定日期加上指定天数
     *
     * @param date 日期
     * @param day  天数
     * @return 返回相加后的日期
     */
    public static Date addDate(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
        return c.getTime();
    }

    /**
     * 增加天数
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDays(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, day);
        date = c.getTime();
        return date;
    }

    /**
     * 增加天数
     *
     * @param date
     * @param
     * @return
     */
    public static Date addMonths(Date date, int month) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, month);
        date = c.getTime();
        return date;
    }

    /**
     * 功能：指定日期加上指定天数
     *
     * @param date   日期
     * @param minute 分钟
     * @return 返回相加后的日期
     */
    public static Date addMinute(Date date, int minute) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) minute) * 60 * 1000);
        return c.getTime();
    }

    /**
     * 功能：添加指定秒杀的时间
     *
     * @param date
     * @param second
     * @return
     */
    public static Date addSecond(Date date, int second) {
        long s = date.getTime();
        s = s + second * 1000;
        return new Date(s);
    }


    /**
     * 功能：分钟相差 minute的时间值
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date getDateByMinuteAdd(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * 功能:两个日期相隔天数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 返回相减后的日期
     */
    public static int diffDate(Date startDate, Date endDate) {
        long endMillis = endDate.getTime();
        long startMillis = startDate.getTime();
        long s = (endMillis - startMillis) / (24 * 3600 * 1000);
        return (int) s;
    }

    /***
     *
     * @Title: diffMinute
     * @author: echao.wang Create at: 2014年9月15日 下午2:40:51
     * @Description: 计算两个时间间隔的分钟数
     * @param @param startDate
     * @param @param endDate
     * @param @return
     * @return int
     */
    public static int diffMinute(Date startDate, Date endDate) {
        long endMillis = endDate.getTime();
        long startMillis = startDate.getTime();
        long s = (endMillis - startMillis) / (60 * 1000);
        return (int) s;
    }

    /**
     * 功能：传入时间按所需格式返回时间字符串
     *
     * @param date   java.util.Date格式
     * @param format yyyy-MM-dd HH:mm:ss | yyyy年MM月dd日 HH时mm分ss秒
     * @return
     */
    public static String format(Date date, String format) {
        String result = "";
        try {
            if (date == null) {
                date = new Date();// 如果时间为空,则默认为当前时间
            }
            if (format == null || format.trim().equals("")) {// 默认格式化形式
                format = "yyyy-MM-dd";
            }
            DateFormat df = new SimpleDateFormat(format);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            result = df.format(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 功能：传入时间按所需格式返回时间字符串
     *
     * @param date   java.util.Date格式
     * @param format yyyy-MM-dd HH:mm:ss | yyyy年MM月dd日 HH时mm分ss秒
     * @return
     */
    public static String dateToString(Date date, String format) {
        String result = "";
        try {
            if (date == null) {
                return result; // 时间为空,返回空
            }
            if (format == null || format.trim().equals("")) {// 默认格式化形式
                format = "yyyy-MM-dd";
            }
            DateFormat df = new SimpleDateFormat(format);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            result = df.format(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 功能：传入时间字符串按所需格式返回时间
     *
     * @param dateStr 时间字符串
     * @param format  跟传入dateStr时间的格式必须一样 yyyy-MM-dd HH:mm:ss | yyyy年MM月dd日
     *                HH时mm分ss秒
     * @return
     */
    public static Date format(String dateStr, String format) {
        if (dateStr == null || dateStr.trim().equals("")) {
            return new Date();
        }
        if (format == null || format.trim().equals("")) {
            format = "yyyy-MM-dd";
        }
        Date date = null;
        try {
            DateFormat f = new SimpleDateFormat(format);
            date = f.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

    /**
     * 功能：时间字符串格式转换
     *
     * @param dateStr  时间字符串
     * @param format   时间字符串的格式
     * @param toFormat 格式为的格式
     * @return
     */
    public static String format(String dateStr, String format, String toFormat) {
        return format(format(dateStr, format), toFormat);
    }

    /**
     * 功能：格式化rss的时间 输入:
     *
     * @param date
     * @return
     */
    public static String formatRssDate(Date date) {
        if (date == null) {
            date = new Date();// 如果时间为空,则默认为当前时间
        }
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        SimpleTimeZone zone = new SimpleTimeZone(8, "GMT");
        sdf.setTimeZone(zone);
        return sdf.format(date);
    }

    /**
     * 功能：返回年
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);

    }

    /**
     * 功能：返回月
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 功能：返回日
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 功能：返回小时
     *
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 功能：返回分
     *
     * @param date
     * @return
     */
    public static int getMinute(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MINUTE);
    }

    /**
     * 功能：返回星期 1：星期一，2:星期二 ... 6:星期六 7:星期日
     *
     * @param date
     * @return
     */
    public static int getChinaWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (week == 0) {
            return 7;
        } else {
            return week;
        }
    }

    /**
     * 功能：返回秒
     *
     * @param date
     * @return
     */
    public static int getSecond2(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.SECOND);
    }

    /**
     * 功能：返回毫秒
     *
     * @param date
     * @return
     */
    public static long getMillis(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    /**
     * 功能：获取当前月的第一天日期
     *
     * @return
     */
    public static Date getMonFirstDay() {
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.set(getYear(date), getMonth(date) - 1, 1);
        return c.getTime();
    }

    /**
     * 功能：获取当前月的最后一天日期
     *
     * @return
     */
    public static Date getMonLastDay() {
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.set(getYear(date), getMonth(date), 1);

        c.setTimeInMillis(c.getTimeInMillis() - (24 * 3600 * 1000));
        return c.getTime();
    }

    /**
     * 指定某个月的第一天
     *
     * @param date
     * @return
     */
    public static Date getTimeMonFirstDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(getYear(date), getMonth(date) - 1, 1);
        return c.getTime();
    }

    /**
     * 指定某个月的最后一天
     *
     * @param date
     * @return
     */
    public static Date getTimeMonLastDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(getYear(date), getMonth(date), 1);
        c.setTimeInMillis(c.getTimeInMillis() - (24 * 3600 * 1000));
        return c.getTime();
    }


    /**
     * 功能：获取当前日期 格式:20101010
     *
     * @return
     */
    public static String getCurrentDate() {
        Date date = new Date();
        return dateFormat.format(date);
    }

    /**
     * 功能：根据生日获取年龄
     *
     * @return
     * @throws ParseException
     */
    public int[] BirthdayGetAge(String birthday) throws ParseException {

        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date birday = sdf.parse(birthday);// 当前对当前的情况
        return this.BirthdayGetAge(birday);
    }

    /**
     * 功能：根据生日获取年龄
     *
     * @return
     */
    public int[] BirthdayGetAge(Date birday) throws ParseException {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String mDateTime = formatter.format(cal.getTime());// 当前时间
        java.util.Date today = formatter.parse(mDateTime);
        int age = today.getYear() - birday.getYear();
        int MM = today.getMonth() - birday.getMonth();
        int day = today.getDate() - birday.getDate();
        if (MM < 0) {
            MM = 12 + MM;
        }
        if (day < 0) {
            MM = MM - 1;
            day = 31 + day;
        }
        if (today.getMonth() == birday.getMonth()

                && today.getDate() == birday.getDate()

                && birday.getYear() % 4 != 0 && today.getYear() != 0

                && birday.getMonth() != 1 && today.getMonth() != 1) {// 月份和日期都与当前时间相同(除去生日和当前年是闰年，并且是二月的情况)

        } else if (today.getMonth() < birday.getMonth()) {// 生日的月份大于当前时间的月份

            age = age - 1;

        } else if (birday.getMonth() == 1 && birday.getDate() == 29// 生日是闰年，当前年不一定是闰年

                && today.getMonth() == 1) {// 生日是闰年的情况,并且本月是二月

            if (today.getYear() % 4 == 0) {// 当前年是闰年 2月有二十九
                if (today.getDate() < birday.getDate()) {//
                    age = age - 1;
                }
            } else {// 当前是年二月是二十八天
                if (today.getDate() < birday.getDate() - 1) {
                    age = age - 1;
                }
            }
        } else if (today.getMonth() == 1 && today.getDate() == 29

                && birday.getMonth() == 1) { // 当前年是闰年，生日年不一定是闰年

            if (birday.getYear() % 4 == 0) {// 生日年是闰年 二月有二十九天

                if (today.getDate() < birday.getDate()) {//

                    age = age - 1;
                }
            } else {// 生日年二月是二十八天

                if (today.getDate() + 1 < birday.getDate()) {

                    age = age - 1;

                }
            }
        } else if (today.getMonth() > birday.getMonth()) {// 不是同一年，生日的月份不大于当前的月份的情况

        } else if (today.getDate() < birday.getDate()) {// 正常的日期(非闰年) 当前 小于 出生
            age = age - 1;
        } else if (today.getDate() == birday.getDate()) {// 当前 等于 出生

        } else {
        }
        System.out.println(age + "岁，" + MM + "月，" + day + "天");
        return new int[]{age, MM, day};
    }

    /**
     * 获取两个时间的间隔，只精确到分钟，如果end早于start，则返回"小于一分钟"
     *
     * @param start
     * @param end
     * @return
     */
    public static String getInterval(Date start, Date end) {
        long l = end.getTime() - start.getTime();
        // long day=l/(24*60*60*1000);
        long hour = (l / (60 * 60 * 1000));
        long min = ((l / (60 * 1000)) - hour * 60);
        long s = (l / 1000 - hour * 60 * 60 - min * 60);
        if (hour < 0 || min < 0) {
            return "0:0:0";
        }
        /*
         * String hourStr = hour+""; if(hour<10){ hourStr = "0"+hourStr; }
         */
        String minStr = min + "";
        if (min < 10) {
            minStr = "0" + min;
        }
        String sStr = s + "";
        if (s < 10) {
            sStr = "0" + s;
        }

        return hour + ":" + minStr + ":" + sStr;
    }

    /**
     * date需要已yyyy-MM-dd方式传值 根据传来的时间date，得到之后的amount天 2014-05-31，amount
     * =1则返回2014-06-01
     *
     * @param date
     * @return
     */
    public static String getAfterDate(String date, int amount) {
        if (date == null || "".equals(date)) {
            return null;
        }
        Calendar ca = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            ca.setTime(sdf.parse(date));
            ca.add(Calendar.DATE, amount);
            String tomorrow = DateFormatUtils.format(ca, "yyyy-MM-dd");
            return tomorrow;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 计算两个时间相差的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int daysBetween(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 时间转换成yyyyMMddHHmmss格式
     *
     * @param date
     * @return
     */
    public static String dateToyyyyMMddHHmmss(Date date) {
        String result = "";
        try {
            if (date == null) {
                return result; // 时间为空,返回空
            }
            DateFormat df = yyyyMMddHHmmssDateTimeFormat;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            result = df.format(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 时间戳转换成yyyy-MM-dd hh:mm:ss时间格式
     *
     * @param timestamp
     * @return
     */
    public static String timestampToTimeStr(long timestamp) {

        String date = dataTimeFormat.format(new Date(timestamp));
        return date;
    }

    /**
     * 时间戳转换成yyyy-MM-dd hh:mm:ss时间格式
     *
     * @param timestamp
     * @return
     */
    public static Date timestampToTime(long timestamp) {

        String date = dataTimeFormat.format(new Date(timestamp));
        return parseDateTime(date);
    }

    /**
     * 时间戳转换成yyyy-MM-dd hh:mm:ss时间格式
     *
     * @param timestamp
     * @return
     */
    public static Date timestampToTime(String timestamp) {

        String date = dataTimeFormat.format(new Date(Long.parseLong(timestamp)));
        return parseDateTime(date);
    }

    /**
     * 对比两个时间相差几分钟
     *
     * @param starttime
     * @param endtime
     * @return
     */
    public static long betweenTimes(Date starttime, Date endtime) {

        long seconds = endtime.getTime() - starttime.getTime();

        return seconds / 60 / 1000;
    }

    /**
     * 两个日期相差天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int betweenDays(Date date1, Date date2) {

        if (date1 == null || date2 == null) {
            return 0;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 校验是否合适时间
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean isLegalTime() {


        Date date = new Date();
        Date date_morning_begin = (Date) date.clone();
        Date date_morning_end = (Date) date.clone();
        Date date_afternoon_begin = (Date) date.clone();
        Date date_afternoon_end = (Date) date.clone();

        date_morning_begin.setHours(9);
        date_morning_begin.setMinutes(0);
        date_morning_begin.setSeconds(0);

        date_morning_end.setHours(11);
        date_morning_end.setMinutes(30);
        date_morning_end.setSeconds(0);

        date_afternoon_begin.setHours(13);
        date_afternoon_begin.setMinutes(0);
        date_afternoon_begin.setSeconds(0);

        date_afternoon_end.setHours(15);
        date_afternoon_end.setMinutes(0);
        date_afternoon_end.setSeconds(0);

        if ((date.after(date_morning_begin) && date.before(date_morning_end))
                || (date.after(date_afternoon_begin) && date.before(date_afternoon_end))) {
            return true;
        }
        return false;
    }


    /**
     * 获取指定周 (2020-01) 的第一天(星期一 2019-12-30)
     *
     * @param string 周期字符串
     * @return yyyy-MM-dd格式字符串
     */
    public static String getFirstDayOfWeek(String string) {
        try {
            Date parse = formatWW.parse(string);
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.set(Calendar.DAY_OF_WEEK, 2);
            String format2 = dateFormat.format(c.getTime());
            return format2;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取指定周的 最后一天
     *
     * @param string 周期字符串
     * @return yyyy-MM-dd格式字符串
     */
    public static String getLastDayOfWeek(String string) {
        try {
            Date parse = formatWW.parse(string);
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.set(Calendar.DAY_OF_WEEK, 2);
            c.add(Calendar.DAY_OF_YEAR, 6);
            String format2 = dateFormat.format(c.getTime());
            return format2;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取上个月
     *
     * @param s
     * @return
     */
    public static String getLastMonth(String s) {
        try {
            Calendar c = Calendar.getInstance();
            //获取月数据
            c.setTime(dateMonthFormatYYYY_MM.parse(s));
            c.add(Calendar.MONTH, -1);
            String yesterMonth = dateMonthFormatYYYY_MM.format(c.getTime());
            return yesterMonth;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取昨天
     *
     * @param s
     * @return
     */
    public static String getLastDay(String s) {
        try {
            Calendar c = Calendar.getInstance();
            //获取月数据
            c.setTime(dateFormat.parse(s));
            c.add(Calendar.DATE, -1);
            String yesterMonth = dateFormat.format(c.getTime());
            return yesterMonth;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 获取指定日期之间的 日期集合
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param nday      相隔天数
     * @return
     */
    public static List<String> getNDaysList(String startTime, String endTime, int nday) {
        int ndaycurrent = nday - 1;
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -ndaycurrent);
            Date start = calendar.getTime();
            Date end = new Date();
            if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(endTime)) {
                //如果用户只选择了startTime,endTime为null,startTime + 10的日期
                start = dateFormat.parse(startTime);
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(start);
                calendar1.add(Calendar.DATE, ndaycurrent);
                end = calendar1.getTime();
            } else if (StringUtils.isBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                //如果用户只选择了endTime,startTime为null,endTime - 10的日期
                end = dateFormat.parse(endTime);
                Calendar calendar2 = Calendar.getInstance();
                calendar2.setTime(end);
                calendar2.add(Calendar.DATE, -ndaycurrent);
                start = calendar2.getTime();
            } else if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                //如果用户选择了startTime和endTime，判断endTime - startTime两个日期是否超过了ndaycurrent，超过返回最近nday天记录
                Date start1 = dateFormat.parse(startTime);
                Date end1 = dateFormat.parse(endTime);
                int a = (int) ((end1.getTime() - start1.getTime()) / (1000 * 3600 * 24));
                if (a <= ndaycurrent) {
                    //如果小于等于n天
                    start = dateFormat.parse(startTime);
                    end = dateFormat.parse(endTime);
                }
            }
            //如果超过了ndaycurrent天,就是默认的start和end
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)

            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 获取指定月的前 size 月份集合
     *
     * @param date 指定日期
     * @param size size
     * @return
     */
    public static List<String> getLastMonths(String date, int size) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(date));
            List<String> list = new ArrayList(size);
            for (int i = 0; i < size; i++) {
                c.setTime(sdf.parse(date));
                c.add(Calendar.MONTH, -i);
                Date m = c.getTime();
                list.add(sdf.format(m));
            }
            Collections.reverse(list);
            return list;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    // 获取当前时间所在年的最大周数
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

        return getWeekOfYear(c.getTime());
    }

    // 获取当前时间所在年的周数
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);

        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 指定日期年月日 时间集合
     *
     * @param startDate
     * @param endxDate
     * @return
     */
    public static List<String> getTimeList(String startDate, String endxDate) {
        SimpleDateFormat sdf;
        int calendarType;

        switch (startDate.length()) {
            case 10:
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                calendarType = Calendar.DATE;
                break;
            case 7:
                sdf = new SimpleDateFormat("yyyy-MM");
                calendarType = Calendar.MONTH;
                break;
            case 4:
                sdf = new SimpleDateFormat("yyyy");
                calendarType = Calendar.YEAR;
                break;
            default:
                return null;
        }

        List<String> result = new ArrayList<>();
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        try {
            min.setTime(sdf.parse(startDate));
            min.add(calendarType, 0);
            max.setTime(sdf.parse(endxDate));
            max.add(calendarType, 1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(min.getTime()));
            curr.add(calendarType, 1);
        }
        return result;
    }


    /**
     * 功能：传入时间按所需格式返回时间字符串
     *
     * @param date   java.util.Date格式
     * @param format yyyy-MM-dd HH:mm:ss | yyyy年MM月dd日 HH时mm分ss秒
     * @return
     */
    public static String dateToStringTwo(Date date, String format) {
        String result = "";
        try {
            if (date == null) {
                return result; // 时间为空,返回空
            }
            if (format == null || format.trim().equals("")) {// 默认格式化形式
                format = "yyyy-MM-dd";
            }
            DateFormat df = new SimpleDateFormat(format, Locale.SIMPLIFIED_CHINESE);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            result = df.format(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
